<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
</head>
<body>
<script>
	var ss = ajax({
	  url:"ajax.php",
	  type:"get",
	  data:"username=zhangsan",
	  success:function (info) {
	    console.log(info);
	  }
	});
	//编写一个方法用来发送请求，接收数据
	function ajax(options) {
		//底层是封装的XMLHttpRequest
	    var xhr = new XMLHttpRequest();

	    if(options.type.toLowerCase()==="get"){
		    options.url += "?"+options.data;
		    options.data = null;
	    }
		xhr.open(options.type,options.url);
	    //如果是post提交
		if(options.type.toLowerCase()==="post"){
			xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
		}
		xhr.send(options.data);
	    //接收数据
	    xhr.onreadystatechange = function () {
	        if(xhr.status ===200 && xhr.readyState===4){
		        options.success(xhr.responseText)
	        }
	    }
	}
</script>
</body>
</html>